/**
 * 消息提示框
 * @param { string } title
 * @param { number } [duration = 3000] - 展示时长
 * @param { string } [icon = "none"] - 是否需要显示icon,可选值如下：
 * "success" - 显示成功图标，此时 title 文本最多显示 7 个汉字长度。
 * "error" - 显示错误图标，此时 title 文本最多显示 7 个汉字长度。
 * "loading" - 显示加载图标，此时 title 文本最多显示 7 个汉字长度。
 * "none" - 为默认值，不显示图标，此时 title 文本在小程序最多可显示两行，App仅支持单行显示。
 * @returns
 */
const showToast = (title, duration = 3000, icon = "none") => {
  if (!title) {
    return;
  }

  uni.showToast({
    title,
    icon,
    duration,
  });
};

/**
 * uniapp uni.showModal()
 * @param { string } content - modal content
 * @param { string } [title = "提示"] - modal title
 * @param { boolean } [showCancel = true] - show cancel button?
 * @returns
 */
const showModal = (content, title = "提示", showCancel = true) => {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: title,
      content: content,
      showCancel: showCancel,
      success: ({ confirm, cancel }) => {
        if (confirm) {
          resolve(true);
        }
      },
    });
  });
};

/**
 * uniapp uni.showLoading()
 * @param { string } title
 * @param { boolean } [mask = true] - show mask?
 */
const showLoading = (title, mask = true) => {
  uni.showLoading({
    title: title,
    mask: mask,
  });
};

/**
 * 小程序拉起微信扫码
 * 详细参数 @see {@link https://uniapp.dcloud.io/api/system/barcode?id=scancode}
 * @returns
 */
const scanCode = () => {
  return new Promise((resolve, reject) => {
    uni.scanCode({
      success: (res) => {
        resolve(res);
      },
      fail: (err) => {
        reject(err);
      },
    });
  });
};

export { showToast, showModal, showLoading, scanCode };
